
package horde.standard.resources.graphics;

import java.awt.Color;
import java.awt.Component;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;

/**
 * @author Andres Picazo
 */
public class TableLabel extends JLabel implements TableCellRenderer {

    // <editor-fold defaultstate="collapsed" desc="Methods to keep the background when not selected">

    private Color defaultBackground;
    public TableLabel () {
        super ("NONE");
        defaultBackground = Color.white;
    }

    public TableLabel (String text) {
        super(text);
        defaultBackground = Color.white;
    }

    public TableLabel (String text, Color c) {
        super(text);
        this.defaultBackground = c;
    }

    public TableLabel (Color c) {
        super("-");
        this.defaultBackground = c;
    }

    @Override public void setBackground (Color color) {
        super.setBackground(color);
        defaultBackground = color;        
    }

    // </editor-fold>

    /* This method needs to replicate the value it gets, quite annoying but works... */
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        TableLabel old = (TableLabel) value;
//        JLabel label = new JLabel(old.getText());
//        label.setForeground(old.getForeground());
//        label.setBackground(old.getBackground());
//        label.setOpaque(old.isOpaque());
//        label.setToolTipText(old.getToolTipText());
//        label.setHorizontalAlignment(old.getHorizontalAlignment());
//        label.setVerticalAlignment(old.getVerticalAlignment());
//        label.setPreferredSize(old.getPreferredSize());
//        label.setBorder(old.getBorder());
//        label.setIcon(old.getIcon());
//        if (isSelected) label.setBackground(defaultBackground);
//        else label.setBackground(((TableLabel)old).defaultBackground);
        /* NEW */
        if (isSelected) old.setBackground(defaultBackground);
        else old.setBackground(old.defaultBackground);
        return (TableLabel) old;
    }
}
